/*
 * Golazo GeoNotes
 * Copyright (C) 2009 Nakajiman Software Inc.
 * http://nakajiman.lrlab.to/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * serverload
 */
function serverload() {

  // GET 
  Golazo.dispatch(function(request, response, me) {

    var id = request.data.id || '';
	if (!Golazo.checkDigit(id))
      throw 'Bad Request';

    var note = Golazo.Note.find(id);
	if (!note)
      throw 'Not Found';

    var modules = note.getModuleList();

    note = note.objectify(true);
	note.user.isMe = note.user.id === (me && me.id); 

    $('meta[name=id]').attr('content', note.id);
    $('meta[name=title]').attr('content', note.title);
    $('meta[name=description]').attr('content', note.description);

    if (me && me.id)
	  $(document).me(me);

    $(document).you(note.user);

    $.each(modules, function() {
      $('#gadget'+this.gadgetId).remove();
	});

    if ($('#gadgets div.gadget').length > 0)
	  $('#hint').remove();

    // When !signin or !signup, replace createModule with signin.html
    if (!me || !me.id)
      $('#gadgets div.gadget a.addUrl').each(function() {
        $(this).attr('href', '/signin.html?url=' + encodeURIComponent(request.uri))
		  .removeClass('addUrl');
	  });

    $('#gadgetsFooter a').attr('href', '/notes/'+note.id+'.html');

  // POST
  }, function(request, response, me) {
    throw 'Method Not Allowed';
  });

};

/**
 * load
 */
function load() {

  $('#gadgets div.gadget').createModule($('meta[name=id]').attr('content'));

};
load.runat = 'client';
